<template>
  <el-drawer title="建议内容" size="600px" :with-header="false" :visible.sync="drawer">
    <div class="advice-box">
      <div class="advice-add">
        <div class="advice-title">每日总结配置</div>
        <el-form ref="ruleForm" :model="form" :rules="rules" label-position="right" label-width="80px">
          <el-form-item label="项目名称" prop="projectIdList">
            <div class="project-select" @click="selectProject" v-if="messageType == 'add'">
              <span>
                <span style="color: #606266;" v-if="projectName">{{projectName}}</span>
                <span style="color: #C0C4CC;" v-else>项目</span>
              </span>
              <span v-if="projectName" class="icon" style="color: #C0C4CC;" @click.stop="clearProject"><i class="el-icon-circle-close"></i></span>
            </div>
            <div class="project-select" v-else>
              <span>
                <span style="color: #606266;" v-if="projectName">{{projectName}}</span>
                <span style="color: #C0C4CC;" v-else>项目</span>
              </span>
              <span v-if="projectName" class="icon" style="color: #C0C4CC;"></span>
            </div>
          </el-form-item>
          <div class="advice-title">参数配置</div>
          <el-table :data="argumList">
            <el-table-column label="启用" align="center">
              <template v-slot="scope">
                <el-switch v-model="scope.row.state" />
              </template>
            </el-table-column>
            <el-table-column label="模块" align="center" prop="moduleName" />
            <el-table-column label="模版编号" align="center" prop="indicatorDesc" />
            <el-table-column label="判断规则" align="center" prop="createBy">
              <template v-slot="scope">
                <el-select v-model="scope.row.comparisonOperator" placeholder="大于等于或小于">
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="判定值" align="center">
              <template v-slot="scope">
                <el-input v-model="scope.row.comparisonValue" placeholder="1次" clearable size="small" />
              </template>
            </el-table-column>
          </el-table>
        </el-form>
      </div>
      <div class="advice-submit">
        <el-button type="primary" @click="cancel">取消</el-button>
        <el-button type="primary" @click="submit">确认</el-button>
      </div>
    </div>
    <ProjectConfigModal ref="projectConfigRef" @update="changeProject" />
  </el-drawer>
</template>
<script>
import {addProjectMonitor,editProjectMonitor,getProjectMonitorDetail,listMonitorIndicator} from '@/api/singleraView/dailySummary'
import ProjectConfigModal from '@/views/auditManagement/projectConfiguration/projectConfigModal.vue'
export default {
  data() {
    return {
      drawer: false,
      form: {
        projectIdList: []
      },
      messageType: 'add',
      rules: {
        projectIdList: [
          { type: 'array', required: true, message: '请选择项目', trigger: 'blur' },
        ],
      },
      argumList: [],
      options: [{
          value: 'ge',
          label: '大于等于'
        }, {
          value: 'lt',
          label: '小于'
        }],
      detailData: [],
      projectName: '',
    }
  },
  components: {
    ProjectConfigModal
  },
  methods: {
    init(data){
      if(data){
        this.messageType = 'edit'
        this.projectName = data.projectName
        this.form.projectIdList = [data.projectId]
        this.getDetailData(data.projectMonitorId)
      }else{
        this.projectName = ''
        this.detailData = []
        this.form.projectIdList = []
        this.messageType = 'add'
        this.getMonitorIndicator()
      }
      this.drawer = true
      this.form.fileName = ''
    },
    clearProject(){
      this.projectName = ''
      this.form.projectIdList = []
    },
    getDetailData(id) {
      getProjectMonitorDetail(id).then(res => {
        this.detailData = res.indicatorConditionList
        this.getMonitorIndicator()
      })
    },
    getMonitorIndicator() {
      listMonitorIndicator().then(res => {
        if(res && res.length > 0){
          this.argumList = res.map(item => {
            let isFlag = true;
            if(item.indicatorNo == 'CQ001' || item.indicatorNo == 'CQ002' || item.indicatorNo == 'CQ003' || item.indicatorNo == 'JQ001'){
              isFlag = false;
            }
            return {
              ...item,
              state: isFlag,
              comparisonOperator: 'ge',
              comparisonValue: '0',
            }
          });
          this.handleData()
        }
      })
    },
    handleData() {
      this.argumList.forEach(item => {
        this.detailData.forEach(element => {
            if(item.monitorIndicatorId == element.monitorIndicatorId){
              item.state = element.enableStatus==1?true:false;
              item.comparisonOperator = element.comparisonOperator;
              item.comparisonValue = element.comparisonValue;
              item.projectMonitorIndicatorId = element.projectMonitorIndicatorId;
            }
        })
      })
    },
    cancel() {
      this.drawer = false
    },
    selectProject(){
      this.$refs.projectConfigRef.getInit()
    },
    changeProject(multi){
      this.projectName = multi.name
      this.form.projectIdList = [multi.value]
    },
    handleChange(file){
      this.form.fileName = file.name
    },
    submit() {
      let indicatorConditionList = []
      let isFlag = false
      let isValue = false
      let isDeter = false
      this.argumList.forEach(element => {
        if(!element.comparisonValue.match(/^[0-9]+([.]{1}[0-9]+){0,1}$/) && element.comparisonValue != 0) {
          isDeter = true;
        }
        if(element.state && !element.comparisonOperator){
          isFlag = true
        }
        if(element.state && !element.comparisonValue){
          isValue = true
        }
        indicatorConditionList.push({
          monitorIndicatorId: element.monitorIndicatorId,
          projectMonitorIndicatorId: element.projectMonitorIndicatorId,
          enableStatus: element.state?1:0,
          comparisonValue: element.comparisonValue,
          comparisonOperator: element.comparisonOperator
        })
      });
      if(isDeter == true){
        this.$message.error(`输入判定值不正确`);
        return;
      }
      if(this.form.projectIdList.length==0){
        this.$message.error('请选择项目');
        return;
      }
      if(isFlag){
        this.$message.error('请选择判定规则');
        return;
      }
      if(isValue){
        this.$message.error('请输入判定值');
        return;
      }
      let params = {
        projectId: this.form.projectIdList[0],
        indicatorConditionList: indicatorConditionList
      }
      if(this.messageType == 'add'){
        addProjectMonitor(params).then(res => {
          this.$emit('update')
          this.drawer = false
        })
      }else {
        editProjectMonitor(params).then(res => {
          this.msgSuccess("编辑成功");
          this.$emit('update')
          this.drawer = false
        })
      }
    }
  }
}
</script>
<style scoped lang="scss">
.advice-box{
  position: relative;
  padding: 20px;
  height: 100%;
  overflow: auto;
  .advice-title{
    font-weight: bold;
    font-size: 18px;
    margin: 20px 0 15px 0;
  }
  .advice-option{
    padding: 10px 0;
    font-size: 15px;
    display: flex;
    flex-direction: row;
    .content{
      flex: 1;
    }
    .left{
      width: 80px;
    }
  }
  .history-box{
    margin-top: 5px;
    display: flex;
    flex-direction: row;
    align-items: center;
    border: 1px solid #bfbfbf;
    padding: 10px;
    border-radius: 5px;
    background: #bfbfbf;
  }
  .advice-submit{
    margin: 20px 0;
    text-align: center;
  }
}
.project-select{
  width: 100%;
  height: 34px;
  background: #FFFFFF;
  border-radius: 5px;
  color: #C0C4CC;
  padding: 0 10px;
  cursor: pointer;
  border: 1px solid #DCDFE6;
  position: relative;
  overflow: hidden;
  .icon{
    position: absolute;
    right: 5px;
    top: 0;
  }
}
.project-box{
  marign: 0;
  padding: 0;
  list-style: none;
  .s-box{
    position: relative;
    float: left;
    display: block;
    margin-right: 5px;
    margin-bottom: 5px;
    padding: 0 30px 0 10px;
    border: 1px solid #DCDFE6;
    border-radius: 5px;
    .el-icon-circle-close{
      position: absolute;
      right: 5px;
      top: 11px;
      cursor: pointer;
      font-size: 16px;
    }
  }
}
::v-deep .el-date-editor.el-input{
  width: 100%;
}
::v-deep .el-upload{
  width: 100%;
}
::v-deep .el-button{
  text-align: left;
}
</style>
